package com.example.demo.controller;

import com.example.demo.service.LoggerService;
import com.example.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/user2")
@Slf4j
public class UserController2 {
    @Autowired
    private UserService userInfo;

    @Autowired
    private LoggerService loggerService;

    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/register")
    public String register(String userName, String password) throws IOException {
        Integer result=userInfo.insert(userName,password);
        log.info("注册成功影响行数为：{}",result);
        //手动回滚
//        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//        try {
//            int a=10/0;
//        }catch (Exception e){
////            log.info("e:{}",e);
////            throw  e;
////            throw new IOException();
////            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//
//        }

        throw new IOException();
//        return "用户注册成功";
    }
    @RequestMapping("/r1")
    @Transactional(isolation = Isolation.DEFAULT)
    public  String r1(String userName, String password){
        Integer result=userInfo.insert(userName,password);
        log.info("注册成功影响行数为：{}",result);
        return "用户注册成功";
    }
}
